<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },

        bodySize:{
          label: '纸箱尺寸',
        },
        pieceNumber:{
          label: '只数',
          type: 'number',
          max: 10000,
        },
        parryNumber:{
          label: '格挡',
          type: 'number',
          max: 10000,
        },
        spacerNumber:{
          label: '垫片',
          type: 'number',
          max: 10000,
        },
        boxSource:{
          label:'纸箱供应方式',
          type: 'radio',
          radioBorder: true,
          showInMultiMode: true,
          radioList: [
            {text: '采购', label: 0},
            {text: '客供', label: 1}
          ],
          default: 0
        },
        factoryId: {
          label: '纸箱厂',
          type: 'list',
          placeholder: '请选择纸箱厂',
          showInMultiMode: true,
          list: []
        },
        clientId:{
          label: '客户名称',
          type: 'list',
          placeholder: '客户名称',
          showInMultiMode: true,

          list: []
        },
        orderDetailId: {
          label: '订单',
          type: 'list',
          list: [],
          placeholder: '请输入订单'
        },
        productId: {
          label: '纸箱',
          type: 'list',
          placeholder: '请选择纸箱',
          list: [],
          isNeedAdd: true,
          selectCallback: this.addBoxSelectCallback,
          addNewItemFunc: this.addNewBox
        },
        boxNumber: {
          label: '纸箱数量',
          type: 'number',
          max: 1000000,
          selectCallback: this.amountChanged

        },
        boxPrice: {
          label: '纸箱价格',
          type: 'number',
          max: 1000,
          selectCallback: this.amountChanged

        },
        amount: {
          label: '总价',
          type: 'number',
          max: 100000000
        },
        dayTime: {
          label: '日期',
          type: 'date',
          showInMultiMode: true,
          placeholder: '请选择日期'
        },
        location: {
          label: '存放位置',
          type: 'list',
          list: [],
          placeholder: '请输入存放位置'
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      },
      orderListFilterByBoxInfoId: [],
      orderList: [],
      multiAddBox: false
    }
  },
  methods: {
    addNewBox() {
      const cb = (newValue) => {
        this.initBoxInfoIdList()
        this.dataForm.boxInfoId = newValue.id
      }
      this.$parent.openBoxInfoDialog(cb)
    },
    init(id,multiAdd) {
      this.dataForm.id = id || 0
      this.initProductInfoList('productId')
      this.initClientInfoList()
      this.initFactoryIdList()
      this.initOrderDetailListByBoxAdd()
      this.multiAddBox=multiAdd

      const props = {
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule( 'boxInfoId', 'status'),
        initApi: 'boxApi/boxAddInfo',
        saveApi: 'boxApi/saveBoxAdd',
        updateApi: 'boxApi/updateBoxAdd',
        objectName: 'boxAdd'
      }


      const numberChangedHandle = (row) => {
        const boxNumber = this.R.propOr(0, 'boxNumber', row)

        const boxPrice = this.R.propOr(0, 'boxPrice', row)

        let amount= Number(boxNumber)  * Number(boxPrice)

        row['amount'] =  amount.toFixed(2);
      }

      if (multiAdd) {
        props['multiSaveApi'] = 'boxApi/multipleSaveBoxAdd'

        props['editTableConfig'] = {

          orderDetailId: {
            title: '订单信息',
            editRenderName: 'xVueBoxSearch',
            optionsGet: () => {
              return this.dataFormConfig.orderDetailId.list.map(item => ({label: item.name, value: item.id}))
            },
            valueChangeEvent: (row) => {
              const {orderDetailId} = row
              if (this.R.isNil(orderDetailId) || this.R.isEmpty(orderDetailId)) {
                return
              }
              this.boxInfoIdSelectHandle(orderDetailId, data => {
                // row.bodySize = this.R.pathOr(0, ['boxInfo', 'bodySize'], data)
                row.boxPrice = this.R.pathOr(0, ['orderDetail', 'boxPriceInfo'], data)
                row.productId = this.R.pathOr(0, ['orderDetail', 'productId'], data)
                let purchaseBoxNumber = this.R.pathOr(0, ['orderDetail', 'purchaseBoxNumber'], data)

                this.$message.error('该订单需要采购的纸箱数量为:' + purchaseBoxNumber)

              })
           //   this.boxInfoIdSelectCallback(row)
            }

          },
          productId: {
            title: '产品名称',
            editRenderName: 'xVueBoxSearch',

            optionsGet: () => {
              return this.dataFormConfig.productId.list.map(item => ({label: item.name, value: item.id}))
            },
            valueChangeEvent: (row) => {
              const {productId} = row
              if (this.R.isNil(productId) || this.R.isEmpty(productId)) {
                return
              }
              this.boxInfoIdSelectHandle(productId, data => {
                // row.bodySize = this.R.pathOr(0, ['boxInfo', 'bodySize'], data)
                row.parryNumber = this.R.pathOr(0, ['boxInfo', 'parryNumber'], data)
                row.spacerNumber = this.R.pathOr(0, ['boxInfo', 'spacerNumber'], data)
                row.pieceNumber = this.R.pathOr(0, ['boxInfo', 'pieceNumber'], data)
              })
            //  this.boxInfoIdSelectCallback(row)
            }
          },

          // bodySize:{
          //   title: '尺寸',
          // },
          // parryNumber:{
          //   title:'格挡',
          //   type: 'number',
          // },
          // spacerNumber:{
          //   title: '垫片',
          //   type: 'number',
          // },
          pieceNumber:{
            title: '只数',
            type: 'number',
          },
          boxNumber:{
            title: '数量',
            type: 'number',
            valueChangeEvent: (row) => {
              numberChangedHandle(row)
            }
          },
          boxPrice:{
            title: '单价',
            type: 'number',
            default: 0,
            valueChangeEvent: (row) => {
              numberChangedHandle(row)
            }
          },
          amount:{
            title: '金额',
            type: 'number',
            default: 0
          },
          location:{
            title: '位置',
            type: 'select',
            optionsGet: () => {
              return [
                {label: '1号', value: '1号'},
                {label: '2号', value: '2号'},
                {label: '3号', value: '3号'},
                {label: '4号', value: '4号'},
                {label: '5号', value: '5号'},
                {label: '6号', value: '6号'},
                {label: '7号', value: '7号'},
                {label: '8号', value: '8号'}
              ]
            },
          },

          remark: {
            title: '备注',
            type: 'String'
          }
        }
      }



      this.$refs.dialog.initDialog(props)

      this.dataFormConfig.location.list=[
        {"id":"1号",'name':'1号'},
        {"id":"2号",'name':'2号'},
        {"id":"3号",'name':'3号'},
        {"id":"4号",'name':'4号'},
        {"id":"5号",'name':'5号'},
        {"id":"6号",'name':'6号'},
        {"id":"7号",'name':'7号'},
        {"id":"8号",'name':'8号'}
      ]

    },
    addBoxSelectCallback(value) {
      mRequest('boxApi/boxInfoInfo', {id: value}, (res) => {
        const boxInfo = this.R.path(['data', 'boxInfo'], res)
        this.dataForm['bodySize'] = boxInfo['bodySize']
        this.dataForm['parryNumber'] = boxInfo['parryNumber']
        this.dataForm['spacerNumber'] = boxInfo['spacerNumber']
        this.dataForm['pieceNumber'] = boxInfo['pieceNumber']
      })
      this.boxInfoIdSelectCallback()
    },
    amountChanged() {
      const number = this.R.propOr(0, 'boxNumber', this.dataForm)
      const price = this.R.propOr(0, 'boxPrice', this.dataForm)

      let amount= Number(number)  * Number(price)

      this.dataForm['amount']=  amount.toFixed(2);
    },
    //TODO
    boxInfoIdSelectCallback(row) {
      if (this.multiAddBox) {
        mRequest('orderApi/getOrderDetailListByBoxInfoId', {boxInfoId: row.boxInfoId}, ({data}) => {
          let orderDetailIdList= this.R.propOr([], 'list', data)
          if (!this.R.isEmpty(orderDetailIdList)) {
            row.orderDetailId=orderDetailIdList[0].id
       //     row.boxPrice=orderDetailIdList[0].unitPrice
          }

        })
      }else {
        mRequest('orderApi/getOrderDetailListByBoxInfoId', {boxInfoId: this.dataForm.boxInfoId}, ({data}) => {
          this.dataFormConfig['orderDetailId'].list = this.R.propOr([], 'list', data)
        })
      }
    },
    boxInfoIdSelectHandle(boxInfoId,cb) {
      if (this.R.isNil(boxInfoId) || this.R.isEmpty(boxInfoId)) {
        return
      }
      mRequest('orderApi/orderDetailInfo', {id: boxInfoId}, ({data}) => {
        if (!(cb instanceof Function)) {
          return
        }
        return cb(data)
      })
    }
  }
}
</script>
